﻿// --------------------------------
// <copyright file="Member.cs" company="Pattern Web Solutions">
//     Copyright © Simon Gill 2010. Distributed under the GPL.
// </copyright>
// <author>Simon Gill</author>
// ---------------------------------
// This file is part of League Manager

// League Manager is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.

// League Manager is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with League Manager.  If not, see <http://www.gnu.org/licenses/>.

namespace LeagueManager.Models
{
    using System;
    using System.Linq;

    /// <summary>
    /// Database object for information about a particular member.
    /// </summary>
    public partial class Member
    {
        /// <summary>
        /// Gets a value to show how many matches this member has won.
        /// </summary>
        public int WinCount
        {
            get
            {
                return (from w in this.Wins where !w.Tied select w).Count();
            }
        }

        /// <summary>
        /// Gets a value to show how many matches this member has lost.
        /// </summary>
        public int LossCount
        {
            get
            {
                return (from l in this.Losses where !l.Tied select l).Count();
            }
        }

        /// <summary>
        /// Gets a value to show how many matches this member has drawn.
        /// </summary>
        public int TieCount
        {
            get
            {
                return (from w in this.Wins where !w.Tied select w).Count() + (from l in this.Losses where !l.Tied select l).Count();
            }
        }

        /// <summary>
        /// Gets a comma-seperated list of the display names of this member's favourite games. Not implemented yet.
        /// </summary>
        public string FavouriteGames
        {
            get
            {
                throw new NotImplementedException();
            }
        }

        /// <summary>
        /// Gets the most-recently issued membership number for this member.
        /// </summary>
        public string MembershipNumber
        {
            get
            {
                return (from n in this.MembershipNumbers orderby n.IssueDate descending select n.Number).FirstOrDefault();
            }
        }

        /// <summary>
        /// Gets the standing of this member in a particular month and year.
        /// </summary>
        /// <param name="year">The year of interest.</param>
        /// <param name="month">The month of interest.</param>
        /// <returns>A LeagueStanding object with the current points values and level for that month and year.</returns>
        public int GetLeagueStandingInMonth(int year, int month)
        {
            return (from l in this.LeagueStandings where l.Year == year && l.Month == month select l.Level).FirstOrDefault();
        }

        /// <summary>
        /// Gets the display name for this member including the membership number.
        /// </summary>
        /// <returns>The full display name for this member.</returns>
        public override string ToString()
        {
            return string.Format("{0} ({1})", this.Name, this.MembershipNumber);
        }
    }
}